package com.muyu.product.controller;

import java.util.List;
import java.util.function.Supplier;
import javax.servlet.http.HttpServletResponse;

import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.domain.BrandInfo;
import com.muyu.product.domain.model.CategoryInfoSaveModel;
import com.muyu.product.domain.resp.CategoryCommonElementResp;
import com.muyu.product.domain.resp.CategoryParentCommonElementResp;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.product.domain.CategoryInfo;
import com.muyu.product.domain.req.CategoryInfoQueryReq;
import com.muyu.product.domain.req.CategoryInfoSaveReq;
import com.muyu.product.domain.req.CategoryInfoEditReq;
import com.muyu.product.service.CategoryInfoService;

/**
 * 品类信息Controller
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Api(tags = "品类信息")
@RestController
@RequestMapping("/category")
public class CategoryInfoController extends BaseController {
    @Autowired
    private CategoryInfoService categoryInfoService;

    /**
     * 查询品类信息列表
     */
    @ApiOperation("获取品类信息列表")
    @RequiresPermissions("product:category:list")
    @GetMapping("/list")
    public Result<List<CategoryInfo>> list(CategoryInfo categoryInfo) {
        List<CategoryInfo> list = categoryInfoService.list(categoryInfo);
        return Result.success(list);
    }

    /**
     * 导出品类信息列表
     */
    @ApiOperation("导出品类信息列表")
    @RequiresPermissions("product:category:export")
    @Log(title = "品类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryInfo categoryInfo) {
        List<CategoryInfo> list = categoryInfoService.list(categoryInfo);
        ExcelUtil<CategoryInfo> util = new ExcelUtil<CategoryInfo>(CategoryInfo.class);
        util.exportExcel(response, list, "品类信息数据");
    }

    /**
     * 获取品类信息详细信息
     */
    @ApiOperation("获取品类信息详细信息")
    @RequiresPermissions("product:category:query")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<CategoryInfo> getInfo(@PathVariable("id") Long id) {
        return Result.success(categoryInfoService.getById(id));
    }

    /**
     * 获取品类信息详细信息
     */
    @ApiOperation("获取品类信息共有属性信息")
    @RequiresPermissions("product:category:query")
    @GetMapping(value = "/getTemplateAttribute/{cateGoryId}")
    @ApiImplicitParam(name = "cateGoryId", value = "cateGoryId", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<CategoryCommonElementResp> getTemplateAttributeByCateGoryId(@PathVariable("cateGoryId") Long cateGoryId) {
        return Result.success(categoryInfoService.getTemplateAttributeByCateGoryId(cateGoryId));
    }

    /**
     * 新增品类信息
     */
    @RequiresPermissions("product:category:add")
    @Log(title = "品类信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增品类信息")
    public Result<String> add(@RequestBody CategoryInfoSaveReq categoryInfoSaveReq) {
        return toAjax(categoryInfoService.save(
                CategoryInfoSaveModel.saveBuild(categoryInfoSaveReq, SecurityUtils::getUsername)
        ));
    }

    /**
     * 修改品类信息
     */
    @RequiresPermissions("product:category:edit")
    @Log(title = "品类信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改品类信息")
    public Result<String> edit(@PathVariable Long id, @RequestBody CategoryInfoEditReq categoryInfoEditReq) {
        return toAjax(categoryInfoService.updateById(CategoryInfo.editBuild(id,categoryInfoEditReq)));
    }

    /**
     * 删除品类信息
     */
    @RequiresPermissions("product:category:remove")
    @Log(title = "品类信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除品类信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
    public Result<String> remove(@PathVariable List<Long> ids) {
        return toAjax(categoryInfoService.removeBatchByIds(ids));
    }

    /**
     * 通过品类ID获取父级以上的属性、属性组、品牌集合
     * @param categoryId 品类ID
     * @return 父级以上的属性、属性组、品牌集合
     */
    @GetMapping("/parentCommonElement/{categoryId}")
    @ApiOperation("通过品类ID获取父级以上的属性集合")
    @ApiImplicitParam(name = "categoryId", value = "categoryId", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class, example = "1")
    public Result<CategoryParentCommonElementResp> parentCommonElement(
            @PathVariable(value = "categoryId") Long categoryId
    ) {
        return Result.success(categoryInfoService.parentCommonElement(categoryId));
    }
}
